package com.lkxicy.wos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lkxicy.wos.pojo.entity.Staff;
import com.lkxicy.wos.mapper.StaffMapper;
import com.lkxicy.wos.service.StaffService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lkxicy
 * @since 2020-11-30
 */
@Service
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements StaffService {

    @Autowired
    private JedisPool jedisPool;

    @Autowired
    private StaffMapper staffMapper;

    @Override
    public Map<Object,Object> searchPageStaff(int pageNum,int limit) {
        // 分页插件的使用   第一个参数是 当前页    第二个参数是  页面大小
        Page<Staff> page=new Page<>(pageNum,limit);
        QueryWrapper<Staff> wrapper=new QueryWrapper<>();
        wrapper.eq("deleted","0");
        wrapper.ne("staffNumber","admin");
        wrapper.ne("staffNumber","superAdmin");
        Map<Object,Object> map=new HashMap<>();
        Page<Staff> temp=staffMapper.selectPage(page, wrapper);
        map.put("data",temp.getRecords());
        map.put("total",temp.getTotal());
        return map;
    }

    @Override
    public List<Staff> searchAllStaff() {
        QueryWrapper<Staff> wrapper=new QueryWrapper<>();
        wrapper.eq("deleted","0");
        wrapper.ne("staffNumber","admin");
        wrapper.ne("staffNumber","superAdmin");
        return staffMapper.selectList(wrapper);
    }

    @Override
    public String changeStaff(Staff staff) {
        staffMapper.changeStaff(staff.getStaffNumber(),staff.getName(),staff.getDepartment(),staff.getPhoneNumber(),
                staff.getSex(),staff.getEmail(),staff.getSalary(),staff.getId());
        if(staff.getStaffNumber().equals("admin") || staff.getStaffNumber().equals("superAdmin")){
            return "管理员信息修改成功！";
        }
        else{
            return "员工信息修改成功！";
        }
    }

    @Override
    public String deleteStaff(String num) {
        Staff staff=staffMapper.findStaffByNumber(num);
        if(staff!=null){
            staffMapper.deleteById(staff.getId());
            return "删除员工成功！";
        }
        else{
            return "员工信息有误！";
        }
    }

    @Override
    public String addStaff(Staff staff) {
        staffMapper.insert(staff);
        return "增加员工成功！";
    }

    @Override
    public String changeStaffPassword(String num,String pass,String newPass) {
        Staff staff=staffMapper.findStaffByNumber(num);

        if(staff==null){
            return "没有这个用户！";
        }
        else{
            if(staff.getPassword().equals(pass)){
                staffMapper.changeStaffPassword(num,newPass);
                return "修改密码成功！";
            }
            else{
                return "原始密码错误，修改失败！";
            }
        }

    }

    @Override
    public String[] staffLogin(String staffNumber, String password) {
        Staff temp=new Staff();
        temp.setStaffNumber(staffNumber);
        temp.setPassword(password);
        Staff staff=staffMapper.findStaffByNumber(staffNumber);
        String token=UUID.randomUUID().toString().replace("-","").toLowerCase();
        String [] maps=new String[3];
        maps[2]="";
        if(staffNumber==null || staffNumber.equals("")){
            maps[0]="500";
            maps[1]="账号不能为空";
            return maps;
        }
        else if(password==null || password.equals("")){
            maps[0]="500";
            maps[1]="密码不能为空";
            return maps;
        }

        if(staffMapper.staffLogin(staffNumber,password)==null){
            maps[0]="500";
            maps[1]="账号不存在或密码错误，请重新输入";
        }
        else{
            maps[0]="200";
            maps[1]="登录成功";
            maps[2]=token;

            // 把staff转化成字符串然后写入到redis里面并设置过期时间
            ObjectMapper mapper = new ObjectMapper();
            try{
                String jsonRst = mapper.writeValueAsString(staff);
                Jedis jedis = jedisPool.getResource();
                jedis.set(token, jsonRst);
                jedis.expire(token,86400);
                jedis.close();
            }catch(JsonProcessingException e){
                e.printStackTrace();
            }

        }
        return maps;
    }

    /**
     * 需要生成随机的六位数密码返回给用户并放到数据库中
     * @param staffNumber
     * @return
     */
    @Override
    public String resetPassword(String staffNumber) {
        String pas=String.valueOf(Math.round((Math.random()*9+1)*100000));
        staffMapper.changeStaffPassword(staffNumber,pas);
        return pas;
    }

    @Override
    public List<String> selectAllDepartment() {
        return staffMapper.selectAllDepartment();
    }

    @Override
    public Staff searchOneStaff(String staffNumber) {
        QueryWrapper<Staff> wrapper = new QueryWrapper<>();
        wrapper.eq("staffNumber",staffNumber);
        return staffMapper.selectOne(wrapper);
    }


}
